/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package profile.hujia.log.analysis.data;

import profile.hujia.log.analysis.common.ConstantVariable;
import profile.hujia.log.analysis.domain.LogInfo;

import java.util.*;

/**
 * class info
 *
 * @author hujia
 * @date 2018/12/17 14:26
 */
public class LogDataServiceImpl implements LogDataService {
    private Map<String, List<LogInfo>> logMap;
    private String title;

    public LogDataServiceImpl() {
        logMap = new HashMap<>();
    }

    @Override
    public void put(String key, LogInfo log) {
        List<LogInfo> logList = logMap.get(key);
        if (logList == null) {
            logList = new ArrayList<>();
            logMap.put(key, logList);
        }
        logList.add(log);
    }

    @Override
    public StringBuilder load() {
        if (logMap.size() <= 0) {
            return null;
        }
        StringBuilder text = new StringBuilder();
        text.append(ConstantVariable.TITLE_BEGIN);
        text.append(title);
        text.append(ConstantVariable.NEW_LINE);
        text.append(ConstantVariable.RESULT_TABLE_HEAD);
        text.append(ConstantVariable.NEW_LINE);
        text.append(ConstantVariable.RESULT_SPLIT_LINE);
        text.append(ConstantVariable.NEW_LINE);
        Date begin = new Date();
        Date end = new Date(begin.getTime() - 1000000000);
        for (int i = 0; i < ConstantVariable.ROLE_NUMBER; ++i) {
            String key = ConstantVariable.ROLE_NAME + i;
            List<LogInfo> value = logMap.get(key);
            long[] minCostTime = {1000L, 1000L};
            long[] maxCostTime = {0L, 0L};
            double[] sumTime = {0.0, 0.0};
            int[] number = {0, 0};
            for (LogInfo info : value) {
                if (info.getOccurrenceTime().before(begin)) {
                    begin = info.getOccurrenceTime();
                }
                if (info.getOccurrenceTime().after(end)) {
                    end = info.getOccurrenceTime();
                }
                if (info.isHaveData()) {
                    judge(minCostTime, maxCostTime, sumTime, number, info, 0);
                } else {
                    judge(minCostTime, maxCostTime, sumTime, number, info, 1);
                }
            }
            text.append(key);
            text.append(ConstantVariable.RESULT_SEPARATOR);
            for (int j = 0; j < 2; ++j) {
                if (number[j] == 0) {
                    text.append(ConstantVariable.LOG_NON);
                } else {
                    text.append(String.format("%.3f", sumTime[j] / number[j]));
                }
                text.append(ConstantVariable.RESULT_SEPARATOR);
                text.append(number[j]);
                text.append(ConstantVariable.RESULT_SEPARATOR);
                text.append(minCostTime[j]);
                text.append(ConstantVariable.RESULT_SEPARATOR);
                text.append(maxCostTime[j]);
                text.append(ConstantVariable.RESULT_SEPARATOR);
            }
            text.append(ConstantVariable.NEW_LINE);
        }
        text.append(ConstantVariable.NEW_LINE);
        text.append(ConstantVariable.RESULT_END);
        text.append(end.getTime() - begin.getTime());
        return text;
    }

    private void judge(long[] minCostTime, long[] maxCostTime, double[] sumTime, int[] number, LogInfo info, int num) {
        if (minCostTime[num] > info.getCostTime()) {
            minCostTime[num] = info.getCostTime();
        }
        if (maxCostTime[num] < info.getCostTime()) {
            maxCostTime[num] = info.getCostTime();
        }
        sumTime[num] += info.getCostTime();
        ++number[num];
    }

    @Override
    public void clear() {
        logMap.clear();
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }
}
